package com.UI;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.Model.StudentDBModel;
import com.Model.Database.Database;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.AvoidXfermode.Mode;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


/**  Author : Mohammed
 * 
 */

public class CourseList extends ListActivity
{
 private ListView lv1;
 List<String> records;
 List<String> courseName;
 String[] course;
 String s;
 String[] menuItems = {"Edit Course", "Delete Course"};
 protected static final int CONTEXTMENU_EDITITEM = 0;
 protected static final int CONTEXTMENU_DELETEITEM = 1;
 StudentDBModel model;
 private CourseAdapter c_adapter;
 String courseNum;
 String[] sResult;
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
	requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);		
	setContentView(R.layout.course_list);
	getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.course_titlebar);
	
	((TextView)findViewById(R.id.CourseTitle)).setText("Courses");
	
	model = new StudentDBModel(this);
	
	
	findViewById(R.id.back).setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
			CourseList.this.finish();

		}
	});

	
	
	findViewById(R.id.plus).setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
			CourseList.this.finish();
			Intent AddCourse = new Intent();
			AddCourse.setClassName("com.UI", "com.UI.AddCourseActivity");
			startActivity(AddCourse);
			
		}
	});
	
 

    lv1= getListView();

    lv1.setTextFilterEnabled(true);

    refreshCourses();
    
    registerForContextMenu(lv1);

    
    
    lv1.setOnItemClickListener(new OnItemClickListener() {
    	public void onItemClick(AdapterView<?> a, View v, int position, long id) {

    		s = lv1.getItemAtPosition(position).toString();
    		sResult = s.split(" ");
    		courseNum = sResult[0];

        	Intent i = new Intent(CourseList.this,ViewCourseActivity.class);
        	i.putExtra("CourseNo", courseNum);
        	startActivity(i);

    	}
    });
   
}
 
 
 public void onCreateContextMenu(ContextMenu menu, View v,
     ContextMenuInfo menuInfo) {
   //if (v.getId()==R.id.android:list) {
     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
     
     menu.setHeaderTitle(course[info.position]);

     for (int i = 0; i<menuItems.length; i++) {
       menu.add(Menu.NONE, i, i, menuItems[i]);
     }
 
   
   //}
 }
 
 
 @Override
 public boolean onContextItemSelected(MenuItem item) {
   AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
   int menuItemIndex = item.getItemId();
   String menuItemName = menuItems[menuItemIndex];
   String listItemName = course[info.position];
   
	sResult = listItemName.split(" ");
	courseNum = sResult[0];

   // Switch on the ID of the item, to get what the user selected. 

   switch (menuItemIndex) {

           case CONTEXTMENU_EDITITEM:

           	Intent i = new Intent(CourseList.this,EditCourseActivity.class);
        	i.putExtra("CourseNo", courseNum);
        	startActivity(i); 
        	   
        	  refreshCourses();
        	   
        	   return true; // true means: "we handled the event". 
        	   
          case CONTEXTMENU_DELETEITEM:
        	  
        	  // Remove it from the list.

        	  model.deleteCourses(courseNum);


              refreshCourses();

              return true; // true means: "we handled the event". 
        	   
        	   
                  

   }

   return false;
 }
 
 
 public void refreshCourses(){
	 
	    // show course number and course name in list
	    records = model.selectCoursesByNo();
	    courseName = model.selectCoursesByTitle();
	    
	    for(int i=0; i < records.size(); i++){
	    
		    String number = records.get(i);
		    String title = courseName.get(i);
		    String comboTitleNumber = number + "  " + title;   
		    records.set(i,comboTitleNumber);
	    }
	    
	    course = (String[])records.toArray(new String[records.size()]);
	 
        this.c_adapter = new CourseAdapter(this, R.layout.course_row, records);
        setListAdapter(this.c_adapter);
	 
 }
 
 
 
}

